package com.yld.demo.util.kafka;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.yld.demo.entity.OrderList;
import com.yld.demo.entity.OrderListVo;
import com.yld.demo.entity.UserInfo;
import com.yld.demo.service.OrderListService;
import com.yld.demo.service.UserInfoService;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @version 1.0
 * @description：TODO
 * @author：侯泽艺
 * @date 2023/3/20 17:29
 */
@Component
public class KafkaConsumer {
    @Autowired
    private UserInfoService userInfoService;
    @Autowired
    private OrderListService orderListService;


    @KafkaListener(topics = "test", groupId = "test-consumer-group")
    public void AddConsumer(ConsumerRecord<String, String> record) {
        System.out.println("开始消费");
        KafkaData kafkaData = JSON.parseObject(record.value(), KafkaData.class);
        String type = kafkaData.getType();
        if (type.equals("user_add") ) {
            UserInfo userInfo = JSON.parseObject(kafkaData.getData(), UserInfo.class);
            userInfoService.save(userInfo);
            System.out.println("user_add消费");
        }
        if (type.equals("order_add")){
            OrderListVo orderList = JSON.parseObject(kafkaData.getData(), OrderListVo.class);
            OrderList list = new OrderList();
            list.setUserId(orderList.getUserId());
            list.setOrderNo(orderList.getOrderNo());
            list.setProdId(orderList.getProdId());
            list.setAmout(orderList.getAmout());
            list.setEnabled(orderList.getEnabled());
            list.setOrderNum(orderList.getOrderNum());
            Date date1= new Date();
            try {
                date1=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(orderList.getOrderDate());
            } catch (ParseException e) {
                throw new RuntimeException(e);
            }
            list.setOrderDate(date1);
            orderListService.save(list);
            System.out.println("order_add消费");
        }if (type.equals("order_delete")){
            String data = kafkaData.getData();
            System.out.println(Integer.valueOf(data));
            orderListService.delete(Integer.valueOf(data));
            System.out.println("user_delete消费");
        }if (type.equals("order_update")){
            OrderListVo orderList = JSON.parseObject(kafkaData.getData(), OrderListVo.class);
            OrderList list = new OrderList();
            list.setUserId(orderList.getUserId());
            list.setOrderNo(orderList.getOrderNo());
            list.setProdId(orderList.getProdId());
            list.setAmout(orderList.getAmout());
            list.setEnabled(orderList.getEnabled());
            list.setOrderNum(orderList.getOrderNum());
            Date date1= new Date();
            try {
                date1=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(orderList.getOrderDate());
            } catch (ParseException e) {
                throw new RuntimeException(e);
            }
            list.setOrderDate(date1);
            orderListService.UpdateOrderList(list);
            System.out.println("order_update消费");
        }
    }
}
